package script;

import com.zh.freechat.common.req.GroupForm;
import com.zh.freechat.domain.message.entity.ChatGroupPO;
import com.zh.freechat.domain.user.entity.ChatUser;
import com.zh.freechat.domain.user.service.ChatUserService;
import com.zh.freechat.domain.user.service.UserChatService;
import com.zh.webcommon.common.support.SpringContextHolder;

import java.util.List;

import static com.zh.webcommon.CommonConst.GSON;

/**
 * 添加群聊成员的脚本
 * 仅用于表数据为数据时
 *
 * @author Zhou
 * @date 10:41 2025/8/12
 */
public class AddGroupMembersScript {
    static String[] allGroups;

    public static void InitSetGroups() {
        //SELECT CONCAT(group_no, ', ', owner_uid) AS group_data FROM ch_group;
        allGroups = new String[]{
                "7141946333, ch_5fbbd280a73c",
                "8909028571, ch_296e97915583",
                "9679398425, ch_4ebadcbe2f30",
                "2131711613, ch_4394d037f639",
                "6089203909, ch_1ac3d2e3e3df",
                "6619606253, ch_b7c8251b1263",
                "1813888012, ch_897a78720f47",
                "2435643685, ch_371e262b22b9",
                "2079958731, ch_dbea8818b5ef",
                "1065213879, ch_15625f86e09b",
                "6847506713, ch_7dc542eaec2e",
                "2883892385, ch_caedf9de88ea",
                "5741156542, ch_d2fdf65e3c80",
                "6680825146, ch_158e97a8b7e5",
                "6700596881, ch_c68c371b2cc8",
                "1215645200, ch_c67d7f7add6c",
                "1865175337, ch_1169567341e8",
                "6318989988, ch_1bcebc476c09",
                "1012785588, ch_6a767e0ef1e7",
                "1500846748, ch_269b87e106f6",
                "8817980645, ch_e3231f735a3d",
                "0128006007, ch_f36dada73715",
                "5163398845, ch_b47a12e59d7e",
                "6623046291, ch_d28fcf37b5a4",
                "1279014178, ch_35e7a37989a8",
                "5748675652, ch_e0259fcff4fd",
                "1785389885, ch_85e98a9be358",
                "2002157286, ch_332cba36c0b4",
                "3554561859, ch_ebb39bafdafd",
                "8923462918, ch_d9db128bacb3",
                "3091390506, ch_c1da07892895",
                "1399336001, ch_9ff40888f201",
                "1451504147, ch_7be2cd8b6bc2",
                "3911512762, ch_60dacd922a33",
                "8777777243, ch_0e66bfb33bd0",
                "8125081062, ch_0376ad0ee5ff",
                "3740460049, ch_7123a2ecffd8",
                "9642741572, ch_b808e65b441c",
                "8547580120, ch_b2c09e06bc1d",
                "5469222932, ch_33293cec0f3d",
                "9949353476, ch_4fc082887d08",
                "6450167020, ch_4f13bdc9bb74",
                "5646587288, ch_68973ef1eedf",
                "7290603582, ch_c288090b3f7a",
                "3738198472, ch_aa817443e09c",
                "6745043124, ch_44a63d902aca",
                "7973003197, ch_cf22e2d202c2",
                "2135897083, ch_0e47fb7ebe13",
                "1428159636, ch_a81d47ada607",
                "1837253132, ch_2c67d8135967",
                "6299545933, ch_b199b2adb6df",
                "2001153063, ch_b7f7b82686fc",
                "3392711959, ch_308e7f9ee8b6",
                "6253805214, ch_28483bc4e1e1",
                "9918130719, ch_e3231f735a3d",
        };
    }

    static {
        InitSetGroups();
    }
    public static void GroupsAddMembers() {
        for (var it : allGroups) {
            groupAddMembers(it.split(","), AddFriendsScript.getRandFriends(6));
        }
    }

    private static void groupAddMembers(final String[] groupNoAndOwnerUid, List<String> userIds) {
        var userChatService = SpringContextHolder.getBean(UserChatService.class);
        var userService = SpringContextHolder.getBean(ChatUserService.class);

        if (groupNoAndOwnerUid.length < 2) {
            System.err.println("groupNoAndOwnerUid length不能 <2");
            return;
        }

        var groupNo = groupNoAndOwnerUid[0].trim();
        var ownerUid = groupNoAndOwnerUid[1].trim();
        //成员的user list
        userIds.add(ownerUid); //加上群组
        List<ChatUser> userList = userService.findByIds(userIds);

        List<GroupForm.MemberItem> users = userList.stream().map(e -> {
            GroupForm.MemberItem memberItem = new GroupForm.MemberItem();
            memberItem.setUserId(e.getUid());
            memberItem.setAliasName(e.getNickname());
            memberItem.setGroupNo(groupNo);
            return memberItem;
        }).toList();

        userChatService.groupAddMembers(users);
    }

    /**
     * 显示全部的群聊list
     */
    public static void showGroups() {
        for (String it : allGroups) {
            var userChatService = SpringContextHolder.getBean(UserChatService.class);

            var groupNo = it.split(",")[0];
            ChatGroupPO group = userChatService.findGroupByNo(groupNo);
            System.out.println("[groupNo=" + groupNo + "] 群列表 = " + GSON.toJson(group));
        }
    }

}
